package DAO.Impl;

import DAO.SaveDataDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @description: 数据库连接操作层实现类
 * @author: hank
 * @create: 2021-03-28 21:25
 **/
@SuppressWarnings("JavaDoc")
public class SaveDataDAOImpl implements SaveDataDAO {
    private final Connection conn;
    private PreparedStatement pstmt;

    public SaveDataDAOImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public int saveCourse(String course, String Class, String teacher, String classWeek, String room, String time, String id, String name, String year, String major, String week, String lastTime) throws SQLException {
        String sql = "INSERT into course VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
                this.pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, course);
                pstmt.setString(2, Class);
                pstmt.setString(3, teacher);
                pstmt.setString(4, classWeek);
                pstmt.setString(5, room);
                pstmt.setString(6, time);
                pstmt.setString(7, lastTime);
                pstmt.setString(8, id);
                pstmt.setString(9, name);
                pstmt.setString(10, year);
                pstmt.setString(11, major);
                pstmt.setString(12, week);
                int OK = this.pstmt.executeUpdate();
                return OK;
        }catch (SQLException e){
            System.out.println(name+"重复数据");
            return 500;
        }
    }

    @Override
    public int saveEvents(String eventName, Integer eventID, String startTime, String lastTime, String studentID, String week,String room, String detail) throws SQLException {
        String sql = "INSERT into events VALUES (?,?,?,?,?,?,?,?)";
        try {
            this.pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, eventName);
            pstmt.setInt(2, eventID);
            pstmt.setString(3, startTime);
            pstmt.setString(4, lastTime);
            pstmt.setString(5, detail);
            pstmt.setString(6, studentID);
            pstmt.setString(7, week);
            pstmt.setString(8, room);
            int OK = this.pstmt.executeUpdate();
            return OK;
        }catch (SQLException e){
            e.printStackTrace();
            return 500;
        }
    }

    @Override
    public int saveMeetings(String meetingName, String meetingID, String memberID, String detail, String startTime, String lastTime, String groupID) throws SQLException {
        String sql = "INSERT into meetings VALUES (?,?,?,?,?,?,?)";
        try {
            this.pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, meetingName);
            pstmt.setString(2, meetingID);
            pstmt.setString(3, memberID);
            pstmt.setString(4, detail);
            pstmt.setString(5, startTime);
            pstmt.setString(6, lastTime);
            pstmt.setString(7, groupID);
            int OK = this.pstmt.executeUpdate();
            return OK;
        }catch (SQLException e){
            e.printStackTrace();
            return 500;
        }
    }
}
